<?php

namespace App\Http\Common;


use App\Http\Models\Admin\CMS\Category;
use App\Http\Models\Admin\Sys\SysParam;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;

class SeoInit {

    private $post;
    private $paramObj;
    function __construct($post)
    {
        $this->post = $post;
        $params = SysParam::where('type',Config::get('common.system_type_seo'))->get();
        foreach($params as $item){
            $this->paramObj[$item->name] = $item->value;
        }
        $this->paramObj = (object)$this->paramObj;

    }


    public  function  getSeo(){

        if($this->post == null){
            return null;
        }

        if($this->post->use_seo == 1){
            return  array(
                'title'=>$this->post->seo_title,
                'description'=>$this->post->seo_description,
                'keywords'=>$this->post->seo_keywords,
            );
        }
        else{

            $this->fillpost();

            $title = $this->paramObj->site_projectitem_seo_title;
            if($title == null || $title == ""){
                $title = $this->paramObj->site_seo_title;
            }

            $keyword = $this->paramObj->site_projectitem_seo_keywords;
            if($keyword == null || $keyword == ""){
                $keyword =  $this->paramObj->site_seo_keywords;
            }

            $description = $this->paramObj->site_projectitem_seo_description;
            if($description == null || $description == ""){
                $description = $this->paramObj->site_seo_description;
            }

            return  array(
                'title'=>SeoInit::getPostSeo($title),
                'description'=>SeoInit::getPostSeo($description),
                'keywords'=>SeoInit::getPostSeo($keyword),
            );


        }



    }


    public  function getPostSeo($seoStr){
        $keys = array();
        $farray = explode("{",$seoStr);
        if(count($farray) >0){
            foreach($farray as $item){
                $sarray = explode("}",$item);
                if(count($sarray)>1){
                    $keys[] = $sarray[0];
                }
            }
        }
        foreach($keys as $key){

            if($this->post->$key){
                $seoStr =  str_replace("{".$key."}",$this->post->$key,$seoStr);
            }else{
                $seoStr =  str_replace("{".$key."}",$this->paramObj->$key,$seoStr);
            }

        }
        return $seoStr;
    }



    public  function fillpost(){

        //添加分类名称
        $category = Category::find($this->post->category_id);
        if($category){
            $this->post->category_name = $category->name;
        }

        //添加标签名称
        $tags = DB::table('post_tags')
            ->rightJoin('tags', 'post_tags.tag_id', '=', 'tags.id')
            ->where('post_tags.post_id',$this->post->id)
            ->get();
        $tagnames = array();
        if($tags){
            foreach($tags as $tag){
                $tagnames[]=$tag->name;
            }
            $this->post->tag_name =implode(',',$tagnames);
        }
        $this->post->post_title = $this->post->title;
        if($this->post->post_content){
            $this->post->p_content = mb_substr(trim(strip_tags($this->post->post_content)),0,150,'utf-8');
        }
    }
}
?> 
